<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-08-09
 */
namespace app\admin\controller;
use think\Request;
use think\helper\Time;
/**
 * 标签管理
 * @author fld
 *
 */
class TagController extends BaseController{
	/**
	 * 标签列表
	 */
	public function indexOp(){
		$model = model('Base');
		$result = $this->getCondition();
		$field = "id,tag_name,update_time";
		$order = "id asc";
		$list = $model->select_table_by_page('tag',$result['condition'],$field,$order,$result['query']);
		$this->assign("list",$list);
		// 获取分页显示
		$page = $list->render();
		$this->assign('page', $page);
		return $this->fetch();
	}
	
	public function getCondition(){
		$result = array();
		$condition = array();
		$query = array();
		//关键字
		$keyword = input('param.keyword/s');
		if(!empty($keyword)){
			$condition['tag_name'] = array('like','%'.$keyword.'%');
			$query['keyword'] = $keyword;
		}
		$this->assign('keyword',$keyword);
		$result['condition'] = $condition;
		$result['query'] = $query;
		return $result;
	}
	/**
	 * 添加标签信息
	 */
	public function addOp(){
		$model = model('base');
	    if (chksubmit()){
	    	$data = array();
	    	$data['tag_name'] = input('post.tag_name');
	    	$data['update_time'] = time();
	        $res = $model->add_table('tag',$data,0);
	        if($res !== false){
	            model('ActionLog')->action_log("添加","添加标签成功，标签编号=".$res."，标签名称=".input('post.tag_name'));
	            $this->msg = "添加标签成功";
	            $result['error'] = 0;
	        }else{
	            $this->msg = "添加标签失败";
	            $result['error'] = 1;
	        }
	        $result['msg'] = $this->msg;
	        return json($result);
	    }else{
	        return $this->fetch();
	    }
	}
	/**
	 * 编辑标签信息
	 */
	public function editOp(){
		$model = model('base');
		if (chksubmit()){
			$result = array();
			$id = input('post.id');
			$data['tag_name'] = input('post.tag_name');
			$res = $model->update_table('tag',array('id'=>$id),$data,1);
			if($res !== false){
				model('ActionLog')->action_log("编辑","编辑标签成功，标签编号=".$id);
				$this->msg = "编辑标签成功";
				$result['error'] = 0;
			}else{
				$this->msg = "编辑标签失败";
				$result['error'] = 1;
			}
			$result['msg'] = $this->msg;
			return json($result);
		}else{
			//标签信息
			$id = input('param.id');
			$tag_info = $model->find_table('tag',array('id'=>$id));
			$this->assign('vo',$tag_info);
			return $this->fetch();
		}
	}
	/**
	 * 删除标签信息
	 */
	public function deleteOp(){
		$result = array();
		$id = input('param.id');
		$model = model('base');
		$tag_info = $model->find_table('tag',array('id'=>$id),'id,tag_name');
		$res = $model->delete_table('tag',array('id'=>$id));
		if($res !== false){
			$model->delete_table('article_tag',array('tag_id'=>$id));
			model('ActionLog')->action_log("删除","删除标签成功，标签编号=".$tag_info['id']."，标签名=".$tag_info['tag_name']);
			$result['error'] = 0;
		}else{
			$this->msg = "删除标签失败";
			$result['error'] = 1;
		}
		$result['msg'] = $this->msg;
		return json($result);
	}
	
	/**
	 * 检查标签名是否存在
	 */
	public function check_ajaxOp(){
		$tag_name = input('post.tag_name');
		$model = model('base');
		$tag_info = $model->find_table('tag',array('tag_name'=>$tag_name));
		if($tag_info){
			return json(false);
		}else{
			return json(true);
		}
	}
	/**
	 * 检查标签名是否存在
	 */
	public function check_name_ajaxOp(){
		$tag_name = input('post.tag_name');
		$id = input('post.id');
		$model = model('base');
		$condtion = array();
		$condtion['tag_name'] = $tag_name;
		$condtion['id'] = array('neq',$id);
		$Tag_info = $model->find_table('tag',$condtion);
		if($Tag_info){
			return json(false);
		}else{
			return json(true);
		}
	}
	
}